package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/strategyComment")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService commentService;

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @GetMapping("/{strategyId}")//攻略下的数据
    public List<StrategyComment> strategyByComment(@PathVariable Long strategyId){
        return commentService.selectAll(strategyId);
    }


    @GetMapping("/comments/{id}")//评论的内容查询
    public Object getReviewEvaluation(@PathVariable Long id){
        return commentService.getReviewEvaluation(id);
    }

    @GetMapping//标签显示
    public Object getTag(){
        return commentService.getTag();
    }

    @PostMapping("/{strategyId}/comments")//保存
    public Object saveStrategyAndComment(StrategyComment strategyComment, MultipartFile file){
        JSONResult jsonResult = new JSONResult();
        try {
            //保存照片
            if (file !=null && file.getSize()>0){
                String uploadQiniu = SevenUploadUtil.uploadQiniu(file);
                strategyComment.setImgUrls(uploadQiniu);
            }
            User user = UserContext.getCurrentUser();
            strategyComment.setUser(user);
            commentService.saveStrategyAndComment(strategyComment);
            jsonResult.setData(strategyComment.getId());
            Long comment_id = strategyComment.getId();
            long[] tags = strategyComment.getTag().getTags();
            //中间表的保存
            for(Long tagId_id: tags){
                strategyCommentMapper.isnerID(comment_id,tagId_id);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }


}
